package com.arpa.wms.controller;

import cn.jpush.api.report.UsersResult;
import com.arpa.core.web.MediaTypes;
import com.arpa.core.web.Result;
import com.arpa.ntocc.common.common.Page;
import com.arpa.ntocc.common.common.constant.StrConsts;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.ntocc.common.security.annotation.NeedLogin;
import com.arpa.wms.domain.dto.ReceiveRegisterDTO;
import com.arpa.wms.domain.entity.Receive;
import com.arpa.wms.domain.entity.ReceiveItem;
import com.arpa.wms.domain.entity.ReceiveRegister;
import com.arpa.wms.domain.vo.ReceiveItemVO;
import com.arpa.wms.domain.vo.ReceiveRegisterVO;
import com.arpa.wms.service.IReceiveRegisterService;
import com.arpa.wms.service.IReceiveService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 收货登记 Controller
 * </p>
 *
 * @author LEO
 * @since 2020-08-04
 */
@Api(tags = "收货登记")
@RestController
@RequestMapping(value="/receiveRegister", produces = "application/json;charset=UTF-8")
@Log4j2
public class ReceiveRegisterController  {

   @Autowired
   private IReceiveRegisterService iReceiveRegisterService;

   @Autowired
   private IReceiveService iReceiveService;

    /**
     * 获取 收货登记 列表
     *
     * @param receiveRegisterDTO
     * @param request
     * @return
     */
    @ApiOperation(value = "获取 收货登记 列表", notes = "获取 收货登记 列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = ReceiveRegisterVO.class))
    @NeedLogin
    @GetMapping()
    public Result list(ReceiveRegisterDTO receiveRegisterDTO, HttpServletRequest request) {
        try {
            Receive receive = iReceiveService.getOne(new QueryWrapper<Receive>().lambda().eq(Receive::getCode, receiveRegisterDTO.getReceiveCode()).eq(Receive::getGroupCode, UserUtil.getBranchCode()));
            receiveRegisterDTO.setGroupCode(UserUtil.getBranchCode());
            ReceiveRegisterVO receiveRegisterVOSum = iReceiveRegisterService.queryListSum(receiveRegisterDTO);
            List<ReceiveRegisterVO> receiveRegisterVOList = iReceiveRegisterService.queryList(receiveRegisterDTO);

            Page returnPage = new Page(receiveRegisterDTO.getPageNum(),receiveRegisterDTO.getPageSize());
            returnPage.setRecords(receiveRegisterVOList);
            returnPage.setTotal(receiveRegisterVOSum.getTotal());
            returnPage.setSumObject(receiveRegisterVOSum);

            Map<String, Object> result = new HashMap<>();
            result.put("receive", receive);
            result.put("registerList", returnPage);
            return Result.ok().data(result);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 添加 收货登记
     *
     * @param  receiveRegister
     * @param request
     * @return
     */
    @ApiOperation(value = "添加 收货登记 ", notes = "添加 收货登记 ")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PostMapping()
    public Result create(@RequestBody @Validated(ReceiveRegister.Save.class) ReceiveRegister receiveRegister, HttpServletRequest request) {
        try {
            receiveRegister.setGroupCode(UserUtil.getBranchCode());
            Result result = iReceiveRegisterService.saveRegister(receiveRegister);
            if(!result.isOk()){
                return result;
            }
            return Result.ok("添加成功");
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    /**
    * 获取收货登记详情
    *
    * @param code
    * @param request
    * @return
    */
    @ApiOperation(value = "获取收货登记详情", notes = "获取收货登记详情")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
        @ApiImplicitParam(name = "code", value = "收货登记code", paramType = "path", dataType = "String")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = ReceiveRegister.class))
    @NeedLogin
    @GetMapping(value = "/{code}")
    public Result receiveRegister(@PathVariable String code, HttpServletRequest request) {
        try {
            ReceiveRegisterVO entity=iReceiveRegisterService.getOneByCode(code);
            return Result.ok().data(entity);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }


    /**
     * 修改收货登记
     *
     * @param entity 收货登记
     * @param request
     * @return
     */
    @ApiOperation(value = "修改收货登记", notes = "修改收货登记")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PutMapping()
    public Result update(ReceiveRegister entity,HttpServletRequest request) {
        if(StringUtils.isBlank(entity.getCode())){
            return Result.error("code不能为空");
        }
        int updateNum = iReceiveRegisterService.updateByCode(entity);
        if(updateNum == 0){
            return Result.error("没有更新数据！code:"+entity.getCode());
        }else{
            return Result.ok("修改成功,更新"+updateNum+"条数据！");
        }
    }

    /**
     * 删除 收货登记
     *
     * @param codes
     * @param request
     * @return
     */
    @ApiOperation(value = "删除 收货登记 ", notes = "删除 收货登记 ", produces = MediaTypes.JSON_UTF_8)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
        @ApiImplicitParam(name = "codes", value = "codes", required = true, paramType = "query", dataType = "List")
    })
    @NeedLogin
    @DeleteMapping(value = "")
    public Result batchdel(@RequestParam List<String> codes, HttpServletRequest request) {
        try {
            iReceiveRegisterService.remove(new QueryWrapper<ReceiveRegister>().lambda().in(ReceiveRegister::getCode,codes).eq(ReceiveRegister::getGroupCode,UserUtil.getBranchCode()));
            return Result.ok();
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 一键收货
     *
     * @param  receiveItemVOS
     * @param request
     * @return
     */
    @ApiOperation(value = "一键收货", notes = "一键收货")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PostMapping("/batchCreate")
    public Result batchCreate(@RequestBody List<ReceiveItemVO> receiveItemVOS, HttpServletRequest request) {
        try {
            Result result = iReceiveRegisterService.batchCreate(receiveItemVOS);
            if(!result.isOk()){
                return result;
            }
            return Result.ok("收货完成");
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

 }
